package com.icinema.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.UIObject;
import com.icinema.main.AddCinemaSubscription;
import com.icinema.main.AddCinemaView;
import com.icinema.main.AddMovieView;
import com.icinema.main.CreateRatingView;
import com.icinema.main.GeneralMoviesListView;
import com.icinema.main.LoginView;
import com.icinema.main.MoviesListView;
import com.icinema.main.UserRegisterView;
import com.icinema.vo.User;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ICinema implements EntryPoint {

	public CreateRatingView createRatingView = new CreateRatingView();
	public AddMovieView addMovieView = new AddMovieView();
	public MoviesListView moviesListView = new MoviesListView();
	public AddCinemaView addCinemaView = new AddCinemaView();
	public LoginView loginView = new LoginView();
	public AddCinemaSubscription addCinemaSubscriptionView = new AddCinemaSubscription();
	public UserRegisterView userRegisterView = new UserRegisterView();
	public GeneralMoviesListView generalMoviesListView = new GeneralMoviesListView();
	MenuBar menu = new MenuBar();
	private AddCinemaViewCommand addCinemaViewCommand =  new AddCinemaViewCommand();
	public Map<String,UIObject> viewStack = new HashMap<String,UIObject>();
	public List<UIObject> viewsList = new ArrayList<UIObject>();
	MenuItem addMovieMenuItem = new MenuItem("Add Movie",new AddMovieCommand());
	MenuItem createRatingMenuItem = new MenuItem("Create Rating",new CreateRatingCommand());
	MenuItem movieListMenuItem = new MenuItem("Movies Showing",new MovieListViewCommand());
	MenuItem addCinemaMenuItem = new MenuItem("Add Cinema",addCinemaViewCommand);
    MenuItem subscriptionMenuItem = new MenuItem("Show subscription",new AddSubscriptionCommand());
    MenuItem  logoutMenuItem = new MenuItem("Logout",new LogoutCommand());
	public void executeAddCinemaViewCommand(){
		addCinemaViewCommand.execute();
	}
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		

		menu.addItem(addMovieMenuItem);
        menu.addItem(createRatingMenuItem);
        menu.addItem(movieListMenuItem);
        menu.addItem(addCinemaMenuItem);
        menu.addItem(subscriptionMenuItem);
        menu.addItem(logoutMenuItem);
        RootPanel.get("menuContainer").add(menu);
        RootPanel.get("formContainer").add(createRatingView);
        RootPanel.get("formContainer").add(addMovieView);
        RootPanel.get("formContainer").add(moviesListView);
        RootPanel.get("formContainer").add(addCinemaView);
        RootPanel.get("formContainer").add(loginView);
        RootPanel.get("formContainer").add(generalMoviesListView);
        RootPanel.get("formContainer").add(addCinemaSubscriptionView);
        RootPanel.get("formContainer").add(userRegisterView);
        
        
        moviesListView.setICinema(this);
        loginView.setICinema(this);
        generalMoviesListView.setICinema(this);
        userRegisterView.setICinema(this);
        
        viewsList.add(createRatingView);
        viewsList.add(addMovieView);
        viewsList.add(moviesListView);
        viewsList.add(addCinemaView);
        viewsList.add(loginView);
        viewsList.add(generalMoviesListView);
        viewsList.add(addCinemaSubscriptionView);
        viewsList.add(userRegisterView);
        menu.setVisible(false);
        changeView(generalMoviesListView);
	}
	public void showUserPanelCallBack(){
		addMovieMenuItem.setVisible(false);
        createRatingMenuItem.setVisible(false);
        movieListMenuItem.setVisible(false);
        addCinemaMenuItem.setVisible(false);
        
        subscriptionMenuItem.setVisible(true);
        logoutMenuItem.setVisible(true);
        changeView(addCinemaSubscriptionView);
        menu.setVisible(true);
        
	}
	public void showAdminPanelCallBack(){
		addMovieMenuItem.setVisible(true);
        createRatingMenuItem.setVisible(true);
        movieListMenuItem.setVisible(true);
        addCinemaMenuItem.setVisible(true);
        subscriptionMenuItem.setVisible(true);
        logoutMenuItem.setVisible(true);
        changeView(moviesListView);
        menu.setVisible(true);
	}
	public void validLoginCallBack(User user){
		if(user == null){
			changeView(loginView);
		}
	}
	public void changeView(UIObject selectedView){
		for(UIObject view:viewsList){
			if(selectedView.equals(view)){
				view.setVisible(true);
			}
			else{
				view.setVisible(false);
			}
		}
	}
	public void showHomeViewCallBack(){
		menu.setVisible(false);
		changeView(generalMoviesListView);
	}
	public void showLoginViewCallBack(){
		changeView(loginView);
	}
	public void showUserRegistrationViewCallBack(){
		changeView(userRegisterView);
	}
	class AddMovieCommand implements Command{
		@Override
		public void execute() {
			changeView(addMovieView);
		}
	}
	class AddSubscriptionCommand implements Command{

		@Override
		public void execute() {
			changeView(addCinemaSubscriptionView);
		}
		
	}
	class CreateRatingCommand implements Command{

		@Override
		public void execute() {
			changeView(createRatingView);
		}
		
	}
	class MovieListViewCommand implements Command{

		@Override
		public void execute() {
			changeView(moviesListView);
		}
		
	}
	class AddCinemaViewCommand implements Command{
		@Override
		public void execute() {
			changeView(addCinemaView);
		}
	}
	class LogoutCommand implements Command{
		@Override
		public void execute() {
			menu.setVisible(false);
			changeView(generalMoviesListView);
		}
	}
}
